/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ro.unibuc.hobbygroups.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import org.springframework.beans.factory.annotation.Required;

import ro.unibuc.hobbygroups.dao.GenericDAO;
import ro.unibuc.hobbygroups.dao.PicturesDAO;
import ro.unibuc.hobbygroups.domain.Group;
import ro.unibuc.hobbygroups.domain.Picture;
import ro.unibuc.hobbygroups.domain.User;
import ro.unibuc.hobbygroups.domain.UserRole;
import ro.unibuc.hobbygroups.exception.HobbyGroupsException;

/**
 * 
 * @author Root
 */
public class UsersServiceImpl implements UsersService {

	private GenericDAO usersDao;
	private GenericDAO groupsDao;
	private GenericDAO usersRolesDao;
	private PicturesDAO picturesDao;

	public UsersServiceImpl() {
	}

	public User findUser(Long id) {
		return (User) usersDao.find(id);
	}

	public List<User> findAll() {
		return usersDao.findAll();
	}

	public void create(User user) {
		usersDao.create(user);
	}

	public void createRole(UserRole userRole) {
		usersRolesDao.create(userRole);
	}

	/**
	 * Create initial random details. <br/>
	 * Invoked by Spring using the init-method bean argument when defining the
	 * service.
	 */
	public void init() {
		
		List<Group> groups = new ArrayList<Group>();
		String groupsIds[] = new String[] {
				"Photography",
				"Want to see some of the most amazing photography work made by fellow hobbyists just like you? Or just to show off with your awesome work? This is the place! As a member you can also access information about local meets and good locations for photography around you.",
				"Painting",
				"See the most amazing work from other hobby artists, just like you! You can add your work and receive feedback from the group, or even better: joining the full members brings you access to local events and places where you can buy all the tools you need!",
				"Cosplay",
				"Be astonished by some of the most complex cosplay costumes ever! You want to publish an image with your latest cosplay? Piece of cake! Publish it and enjoy the amazement it provokes on other fellow cosplayers. Members can also see all the cosplay meets taking place, and access and update anytime the map with places where you can buy materials from."};

		for (int i = 0; i < groupsIds.length; i += 2) {
			Group group = new Group();

			group.setGroupName(groupsIds[i]);
			group.setDescription(groupsIds[i + 1]);

			groupsDao.create(group);
			groups.add(group);
		}
		
		List<User> users = new ArrayList<User>();
		String userIds[] = new String[] { "luyza", "Luiza Udroiu", "ROLE_USER",
				"georgy", "Georgiana Udrescu", "ROLE_USER", "ellena", "Elena",
				"ROLE_USER", "nyck", "Nicusor Petcu", "ROLE_USER", "admin",
				"Administrator", "ROLE_USER,ROLE_ADMIN" };

		for (int i = 0; i < userIds.length; i += 3) {
			User user = new User();
			user.setUserId(userIds[i]);
			user.setUserName(userIds[i + 1]);
			user.setUserPass("passwd"); // btw this should be an encrypted pass

			Set<Group> userGroups = new HashSet<Group>();
			userGroups.add(groups.get(users.size() % 3));
			
			user.setUserGroups(userGroups);
			
			users.add(user);
			create(user);
			

			// For each user, tokenize and give the corresponding roles
			StringTokenizer st = new StringTokenizer(userIds[i + 2], ",");
			while (st.hasMoreTokens()) {
				UserRole role = new UserRole();
				role.setUserId(userIds[i]);
				role.setAuthority(st.nextToken().trim());

				createRole(role);
			}
		}
		
		

		String pictures[] = new String[] { "496.jpg", "506.jpg", "509.jpg",
				"510.jpg", "518.jpg", "adio_toamna.jpg", "images.jpg",
				"lac-insula-peisaj-natura_1280x1024.jpg", "ld2.jpg",
				"natura_18_L.jpg", "rapsodii20de20toamna.jpg",
				"toamna-1-779562.jpg", "toamna.jpg", "toamna3.jpg",
				"toamna_05.jpg", "toamna_2_1229030210.jpg" };

		for (int i = 0; i < pictures.length; i++) {
			Picture pic = new Picture();

			pic.setInitialName(pictures[i]);
			pic.setType("jpg");
			pic.setDescription("startup photo");
			
			pic.setGroup(groups.get(i % 3));
			pic.setUser(users.get(i % users.size()));
			
			try {
				InputStream is = this.getClass().getResourceAsStream(
						"/startupphotos/" + pictures[i]);

				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				byte buffer[] = new byte[1024];

				int count = is.read(buffer, 0, 1024);
				while (count != -1) {
					baos.write(buffer, 0, count);
					count = is.read(buffer, 0, 1024);
				}

				baos.close();
				is.close();

				picturesDao.create(pic, baos.toByteArray());
				// pic.setContent(baos.toByteArray());
			} catch (IOException e) {
				throw new HobbyGroupsException(e);
			}
		}
	}

	@Required
	public void setUsersDao(GenericDAO usersDao) {
		this.usersDao = usersDao;
	}

	@Required
	public void setGroupsDao(GenericDAO groupsDao) {
		this.groupsDao = groupsDao;
	}

	@Required
	public void setUsersRolesDao(GenericDAO usersRolesDao) {
		this.usersRolesDao = usersRolesDao;
	}

	@Required
	public void setPicturesDao(PicturesDAO picturesDao) {
		this.picturesDao = picturesDao;
	}
}
